<template>
  <div id="app" class="page-wrap">
    <slot v-if="pageShow">
      <router-view class="page"></router-view>
    </slot>
  </div>
</template>

<script>
import { mapState } from 'vuex'
  export default {
    name: 'App',
    data() {
      return {
        pageShow: false,
        allData:''
      };
    },
    mounted () {
/*      if (window.history && window.history.pushState) {
        // 向历史记录中插入了当前页
        history.pushState(null, null, document.URL);
        window.addEventListener('popstate', this.goBack, false);

      }
      window.addEventListener('beforeunload', e => this.beforeunloadHandler(e))
      window.addEventListener('unload', e => this.unloadHandler(e))*/
      this.initPage()
    },
    destroyed () {
/*      window.removeEventListener('popstate', this.goBack, false);
      window.removeEventListener('beforeunload', e => this.beforeunloadHandler(e))
      window.removeEventListener('unload', e => this.unloadHandler(e))*/
    },

    computed: {
      ...mapState(['idInfo'])
    },
    created() {

    },
    methods: {
      //页面初始化
      initPage() {
        //alert(window.location.href)
        let urlParams = {}
        if (window.location.href.indexOf('?') > 0) {
          let urlParamsStr = window.location.href.split('?')[1].split('&')
          for(let i = 0; i < urlParamsStr.length; i ++) {
            urlParams[urlParamsStr[i].split('=')[0]] = urlParamsStr[i].split('=')[1]
          }
        }
        this.$router.push({path: '/Home', replace: true})
          if (urlParams.type == undefined || urlParams.type == '' || urlParams.type == null) {
            urlParams.type = 0
          }
          if(!urlParams.token) {
            urlParams = this.idInfo
          }
          let appAcctId = urlParams.loginName;
          if(urlParams.loginName==undefined||urlParams.loginName == null){
            appAcctId = urlParams.appAcctId;
          }
          let params = {
            token: urlParams.token,
            appAcctId: appAcctId,
            flag: urlParams.flag,
            type: urlParams.type
          }
          this.$http.post('/api/user/getUserInfo', params).then(res => {
            if (res.success == true) {
              let info = res.info
              sessionStorage.setItem("info",res.info);

              if(info.home_city == '100000') {
                sessionStorage.setItem('role', 'all')
              } else if (info.home_city.substr(-4) == '0000') {
                sessionStorage.setItem('role', 'province')
              } else {
                sessionStorage.setItem('role', 'city')
              }
              this.$store.commit('setUserInfo', info)
              this.$store.commit('setIdInfo', urlParams)
              this.pageShow = true
            } else {
              this.$dialog.alert({
                title: '温馨提示',
                message: JSON.stringify(res.msg)
              })
            }
          })

        // let params = {
        //   token: 'm3oj4grmedxygp439vi8hnpgh3ekj0iw',
        //   appAcctId: '306480',
        //   flag: '1',
        //   type: '0'
        // }

       /* let info = {
                       from_priv: null,
                       home_city: "100000",
                       home_city_desc: null,
                       home_county: null,
                       home_county_desc: null,
                       login_seq: null,
                       mail_addr: "jintao@chinatowercom.cn",
                       mobile_phone: "18603659088",
                       nick_name: "yuanxin3",
                       org_id: "100000",
                       org_name: "中国铁塔",
                       password: "bObrajagcdbLcbcbb6b7asbfaEaIbAaqbabObxagahbcbHa6cdagbEbCazcbcdaL",
                       priv_id: null,
                       req_source: null,
                       sec_level: null,
                       token_code: "9m3hw92ku9wa5civfvqgunnjzi0a8p6y",
                       user_id: "1068313",
                       user_name: "金涛",
                       role_code: '1',  //日报
                       role_view: '2', //查看
                       role_update: null, //修改
                     }*/

       /* let info = {
              from_priv: null,
              home_city: "100000",
              home_city_desc: null,
              home_county: null,
              home_county_desc: null,
              login_seq: null,
              mail_addr: "wangyan@chinatowercom.cn",
              mobile_phone: "13581928837",
              nick_name: "wangyan",
              org_id: "1000010900",
              org_name: "信息技术研究院/信息化部",
              password: "mko0(IJN",
              priv_id: null,
              req_source: null,
              role_code: '1',  //日报
              role_view: '2', //查看
              role_update: null, //修改
              role_name: null,
              sec_level: null,
              token_code: "n0fopshgf4eodmvavw3zo9iu5rm88suf",
              user_id: "1068331",
              user_name: "y",
              countLogs: '0',
              solve: '4'
            }*/
        /*let info = {
          countLogs: "0",
          from_priv: null,
          home_city: "100000",
          home_city_desc: null,
          home_county: null,
          home_county_desc: null,
          login_seq: null,
          mail_addr: "suxu@chinatowercom.cn",
          mobile_phone: "18601323969",
          nick_name: "suxu",
          org_id: "1000010500",
          org_name: "人力资源部",
          password: "mko0(IJN",
          priv_id: null,
          req_source: null,
          role_code: null,
          role_name: null,
          role_update: null,
          role_view: null,
          sec_level: null,
          solve: null,
          token_code: "vpqs5mbte2jijdyf46u5dc9i1vxmi2ez",
          user_id: "1068340",
          user_name: "苏旭",
          verify_code: "null",
        }*/
        /*     let info = {
               from_priv: null,
               home_city: "230100",
               home_city_desc: null,
               home_county: null,
               home_county_desc: null,
               login_seq: null,
               mail_addr: "jintao@chinatowercom.cn",
               mobile_phone: "18603659088",
               nick_name: "jintao",
               org_id: "100000",
               org_name: "中国铁塔",
               password: "bObrajagcdbLcbcbb6b7asbfaEaIbAaqbabObxagahbcbHa6cdagbEbCazcbcdaL",
               priv_id: null,
               req_source: null,
               sec_level: null,
               token_code: "9m3hw92ku9wa5civfvqgunnjzi0a8p6y",
               user_id: "1057398",
               user_name: "金涛",
               role_code: '1',  //日报
               role_view: '2', //查看
               role_update: null, //修改
             }*/
        /*if(info.home_city == '100000') {
          sessionStorage.setItem('role', 'all')
        } else if (info.home_city.substr(-4) == '0000') {
          sessionStorage.setItem('role', 'province')
        } else {
          sessionStorage.setItem('role', 'city')
        }
        this.$store.commit('setUserInfo', info)
        this.$store.commit('setIdInfo', urlParams)
        this.pageShow = true*/

      },
      goBack () {
        // console.log("点击了浏览器的返回按钮");
        if(window.location.href.indexOf("Home")>0){
          sessionStorage.clear();
          this.$router.back();
        }
      },
      beforeunloadHandler(){
        this._beforeUnload_time=new Date().getTime();
      },
      unloadHandler(e){
        this._gap_time=new Date().getTime()-this._beforeUnload_time;
        //判断是窗口关闭还是刷新
        if(this._gap_time<=5){
          //如果是登录状态，关闭窗口前，移除用户
          if(sessionStorage.getItem("info")!=null){
            sessionStorage.clear()
          }
        }
      },
    }

  }
</script>
